package com.zhengjiang.cxf.webservice.client;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;

public class ClientUtil {

    public static <T> T getService(String wsdlUri, Class<T> clazz){
        JaxWsProxyFactoryBean jaxWsProxyFactoryBean=new JaxWsProxyFactoryBean();
        jaxWsProxyFactoryBean.setAddress(wsdlUri);
        jaxWsProxyFactoryBean.setServiceClass(clazz);
        T t = (T) jaxWsProxyFactoryBean.create();
        Client proxy= ClientProxy.getClient(t);
        HTTPConduit conduit=(HTTPConduit)proxy.getConduit();
        HTTPClientPolicy policy=new HTTPClientPolicy();
        policy.setConnectionTimeout(30000);//设置连接超时时间
        policy.setReceiveTimeout(30000);//设置响应时间
        conduit.setClient(policy);
        return t;
    }

    public static void main(String[] args) {
//        JaxWsProxyFactoryBean jaxWsProxyFactoryBean=new JaxWsProxyFactoryBean();
//        jaxWsProxyFactoryBean.setAddress("http://localhost:8080/soap/user?wsdl");
//        jaxWsProxyFactoryBean.setServiceClass(UserService.class);
//
//        UserService userService = (UserService) jaxWsProxyFactoryBean.create(); // 创建客户端对象
//        Client proxy= ClientProxy.getClient(userService);
//        HTTPConduit conduit=(HTTPConduit)proxy.getConduit();
//        HTTPClientPolicy policy=new HTTPClientPolicy();
//        policy.setConnectionTimeout(30000);//设置连接超时时间
//        policy.setReceiveTimeout(30000);//设置响应时间
//        conduit.setClient(policy);
        UserService userService = getService("http://localhost:8080/soap/user?wsdl",UserService.class);

        String userName = userService.getUserName("zhang");
        User clientUser = new User(22,"wangwu","male");
        boolean b = userService.addUser(clientUser);


        System.out.println("UserName:"+userName);
        System.out.println(b);
        System.out.println(userService.getAllUser());

    }
}
